/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Juridic;
import org.ipti.br.tag.entity.Juridic_;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.ipti.br.tag.util.CalendarUtil;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Stateless
public class JuridicFacade extends AbstractFacade<Juridic> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public JuridicFacade() {
        super(Juridic.class);
    }

    @Override
    public void create(Juridic juridic) {
        getEntityManager().persist(juridic);
    }

    @Override
    public void edit(Juridic juridic) {
        getEntityManager().merge(juridic);
    }

    public List<Juridic> modifiedJuridicBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Juridic> cq = cb.createQuery(Juridic.class);
        Root<Juridic> from = cq.from(Juridic.class);
        Predicate predicate1 = cb.notEqual(from.get(Juridic_.serialNumber), serialNumber);
        Predicate predicate2 = cb.greaterThanOrEqualTo(from.get(Juridic_.lastModified), initialDate);
        Predicate predicate3 = cb.lessThan(from.get(Juridic_.lastModified), finalDate);
        cq.where(cb.and(predicate1, predicate2, predicate3));

        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<Juridic> findAllOrder() {
        return findAllOrderBy(Juridic_.companyName, Juridic_.fancyName);
    }
}
